const app = getApp();
var api = require('../../api.js');
var mta = require('../../mta_analysis.js');

Page({
  data: {
    page:0,
    list:[
      {
        url:"../courseDetail/courseDetail",
        img:"../../images/test.jpg",
        lesson:"《财道》",
        time:"2018-06-17",
        day:"4",
        place:"广东—广州",
        price:"1000"
      },
      {
        url:"../courseDetail/courseDetail",
        img:"../../images/test.jpg",
        lesson:"《经营之道》",
        time:"2018-06-17",
        day:"4",
        place:"广东—东莞",
        price:"1000"
      },
       
    ]
  },
  onLoad: function (options) {
    var page = this ;
    app.bind();
    page.getCourseList();

    wx.getNetworkType({
      success: function (res) {
        console.log(res)
        page.setData({
          networkType: res.networkType
        })
      }
    });

    mta.Page.init();
  },
  onReady: function () {
  
  },
  onShow: function () {
  
  },
  onHide: function () {
    var url = app.getcurrentPage();
    wx.setStorageSync("from_url", url);
  },
  onUnload: function () {
  
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    app.bind();
    var page = this;

    page.getCourseList();

    // 显示顶部刷新图标  
    wx.showNavigationBarLoading();

    // 隐藏导航栏加载框  
    wx.hideNavigationBarLoading();

    //停止下拉刷新
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    // 显示加载图标  
    wx.showLoading({
      title: '玩命加载中',
    });

    if (that.data.page < that.data.count) {
      that.setData({
        page: that.data.page + 1
      });
    }

    that.getCourseList();
    // 隐藏加载框  
    wx.hideLoading();
  },
  onShareAppMessage: function () {
    var url = app.getcurrentPage();
    var user_id = wx.getStorageSync('user_id');
    console.log(url + "?user_id=" + user_id);
    return {
      path: url + "?user_id=" + user_id,
      title: '得道1314'
    };
  },
  getCourseList:function(){
    var that = this;
    var access_token = wx.getStorageSync('access_token');
    var url = api.three.courseList;
    console.log(access_token);
    console.log(url);
    wx.request({
      url:api.three.courseList,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        page:that.data.page
      },
      success: function (e) {
        if (e.data.code == 0) {
          that.setData({
            list: e.data.data.list,
            row_count: e.data.data.row_count
          })
          console.log(that.data.list)
          console.log(that.data.row_count);
          Math.ceil(that.data.row_count / 10);
          that.setData({
            count: Math.ceil(that.data.row_count / 10)
          })
          console.log(that.data.count)
        } else {
          wx.navigateTo({
            url: '/pages/grant/grant'
          })
        }
      }
    });
  },
  toCourseDetail: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/courseDetail/courseDetail?id='+id
    })
  },

  //跳转到首页
  toIndex: function () {
    wx.switchTab({
      url: '/pages/index/index'
    })
  }
})